class Solution(object):
    def rangeBitwiseAnd(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        def getNBits(m):
            count = 0
            while m:
                m >>= 1
                count += 1
            return count
        nbm = getNBits(m)
        nbn = getNBits(n)
        if nbm!=nbn: return 0
        ret = 0
        for i in range(nbm)[::-1]:
            mask = 1<<i
            if m&mask == n&mask:
                if m&mask: ret |= mask
            else:
                break
        return ret

print(Solution().rangeBitwiseAnd(5,5))